package edu.syr.iis.web.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import javax.persistence.Table;

import edu.syr.iis.web.util.Key;

@Entity
@Table(name="AnnotationSession")
public class AnnotationSession {
	
	int annotationSessionId;

	Date startDateTime;
	Date endDateTime;
	User user;
	
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getAnnotationSessionId() {
		return annotationSessionId;
	}
	
	public void setAnnotationSessionId(int annotationSessionId) {
		this.annotationSessionId = annotationSessionId;
	}

	@Basic
	public Date getStartDateTime() {
		return startDateTime;
	}

	public void setStartDateTime(Date startDateTime) {
		this.startDateTime = startDateTime;
	}

	@Basic
	public Date getEndDateTime() {
		return endDateTime;
	}

	public void setEndDateTime(Date endDateTime) {
		this.endDateTime = endDateTime;
	}

	@ManyToOne
	@JoinColumn(name="userId")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
	@Transient
	public long getDuration()
	{
		if (startDateTime != null && endDateTime != null)
			return endDateTime.getTime() - startDateTime.getTime();
		else	
			return 0;
	}
	
	// Dropping the seconds part, for  annotationsessionlist.jsp 
	//
	@Transient
	public String getStartDateTimeString()
	{
		if (startDateTime != null)
			return startDateTime.toString().substring(0, 16);
		else
			return null;
	}
	
	// Dropping the seconds part, for  annotationsessionlist.jsp 
	//
	@Transient
	public String getEndDateTimeString()
	{
		if (endDateTime != null)
			return endDateTime.toString().substring(0, 16);
		else
			return null;
	}
	
	@Transient
	public String getDurationString()
	{
		long diff = getDuration();
		long days = diff / Key.dayInMillis;
		diff = diff % Key.dayInMillis;
		long hours = diff / Key.hourInMillis;
		diff = diff % Key.hourInMillis;
		long minutes = diff / Key.minuteInMillis;
		diff = diff % Key.minuteInMillis;
//		long seconds = diff / Key.secondInMillis;
		
		String returnString = "";
		
		if (days > 0 ) returnString += (Long.toString(days) + " days ");
		if (hours > 0) returnString += Long.toString(hours) + " hours ";
		returnString += Long.toString(minutes) + " minutues";
				
		return returnString;		       
	}
	
	@Transient	
	public String getSignOutSummary()
	{
		return "Started at: " + startDateTime.toString() + "<br />" +
		       "Ended at:   " + endDateTime.toString() + "<br />" +
		        "Duration:   " + getDurationString() +  "<br />";			   			   
	}
	
	@Transient
	public AnnotationSessionStatistics getStat(List<AnnotationSession> annotationSessions)
	{
		AnnotationSessionStatistics stat = new  AnnotationSessionStatistics();
		
		long totalDuration = 0;		
		int numOfSessions = 0;
		
		
		for (AnnotationSession annotationSession: annotationSessions)
		{
			totalDuration += annotationSession.getDuration();
			numOfSessions++;
			stat.setUser(annotationSession.getUser());
		}
		
		stat.setNumOfSessions(numOfSessions);
		stat.setTotalDuration(totalDuration);
		
		return stat;		
	}
		
	public String toLogString()
	{
		return "Session ID: " + annotationSessionId + "\n" +
		       "User:       " + user.toString() + "\n" +
			   "Started at: " + startDateTime.toString() + "\n" +
			   "Ended at:   " + endDateTime.toString() + "\n" +
			   "Duration:   " + getDurationString() +  "\n";			   			   
	}

}
